import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


public class ScanningClass implements Comparator<String> {

	public static HashMap<String, ArrayList<Integer> > positionMap;
	private static int finalPos;
	public static void main(String[] args) {
		positionMap = new HashMap<String, ArrayList<Integer> >();
		//HoD full text from http://www.fullbooks.com/Heart-of-Darkness.html
		File fileName = new File("src/fullText.txt");
		Scanner s = null;
		try {
			s = new Scanner(fileName);
		}
		catch (FileNotFoundException e) {e.printStackTrace();}
		Pattern p = Pattern.compile("\\w?.*");
		int word = 0;
		String addBefore = "";
		while(s.hasNext(p) ) {
			String strCheck = s.next(p).toLowerCase();
			if(strCheck.matches("\\w?\\-\\n")) {
				addBefore = strCheck.substring(0, strCheck.length()-3);//get rid of all of those annoying words that wrap around to second line
			}
			String[] temp = s.next(p).toLowerCase().split("\\W");
			if(temp.length>0) {
				String str = addBefore+temp[0];
				ArrayList<Integer> positions = new ArrayList<Integer>();
				if(positionMap.get(str)!= null) {
					positions = positionMap.get(str);
				}
				positions.add(word++);
				positionMap.put(str, positions);
				addBefore="";
			}
		}
		finalPos = word;
		
		try {
			outputToExcel(true, "sortedAlphabetically");
			outputToExcel(false, "sortedByOccurance");
		}
		catch (FileNotFoundException e) {e.printStackTrace();}
		catch (IOException e) {e.printStackTrace();}
	}

	private static void outputToExcel(boolean sortByName, String fileName) throws IOException {//install Apache POI to make this work, and add it to the build path (http://poi.apache.org/)
		// create a new file
		FileOutputStream out = new FileOutputStream(fileName+".xls");
		// create a new workbook
		Workbook wb = new HSSFWorkbook();
		// create a new sheet
		Sheet s = wb.createSheet();
		// declare a row object reference
		Row r = null;
		// declare a cell object reference
		Cell c = null;

		// set the sheet name
		wb.setSheetName(0, "Data Sheet" );
		// create a sheet with 30 rows (0-29)
		ArrayList<String> list =new ArrayList<String>(positionMap.keySet());
		if(sortByName) {
			Collections.sort(list);
		}
		else {
			Collections.sort(list, new ScanningClass() );
		}
		
		r = s.createRow(0);
		c = r.createCell(0);
	    c.setCellValue("Text");
	    c = r.createCell(1);
	    c.setCellValue("Total Occurances");
		for (int rownum = 0; rownum <list.size(); rownum++) {
		    // create a row
		    r = s.createRow(rownum+2);
		    c = r.createCell(0);
		    c.setCellValue(list.get(rownum) );
		    c = r.createCell(1);
		    c.setCellValue(positionMap.get(list.get(rownum)).size() );
		    for (int cellnum = 0; cellnum < positionMap.get(list.get(rownum)).size(); cellnum++) {
		        // create a numeric cell
		    	if(cellnum<=253) {
		    		c = r.createCell(cellnum+2);
		    	}
		        //add the percentage of the way through the whole book
		        c.setCellValue( ((double) positionMap.get(list.get(rownum)).get(cellnum))/finalPos*100 );
		    }
		}
		// write the workbook to the output stream
		// close our file (don't blow out our file handles
		wb.write(out);
		out.close();
	}
	
	public int compare(String s0,String s1) {
		return positionMap.get(s1).size()-positionMap.get(s0).size();
	}
}
